package durak.utils;

import durak.logic.UserManager;
import durak.server.control.GamesManager;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;


public class ServletUtils {
	public static UserManager getUserManager(ServletContext servletContext) {
		if (servletContext.getAttribute("userManager") == null) {
			servletContext.setAttribute("userManager", new UserManager());
		}
		return (UserManager) servletContext.getAttribute("userManager");
	}

	public static int getIntParameter(HttpServletRequest request, String name) {
		String value = request.getParameter(name);
		if (value != null) {
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException numberFormatException) {
				return -1;
			}
		}
		return -1;
	}

	public static GamesManager getGamesManager(ServletContext servletContext) {
		if (servletContext.getAttribute("gamesManager") == null) {
			servletContext.setAttribute("gamesManager", new GamesManager());
		}
		return (GamesManager) servletContext.getAttribute("gamesManager");
	}
}
